package com.wswy.wyjk.ui.login.Presenter;

import android.os.CountDownTimer;

import com.my.httpapi.api.baseUtils.Toast;
import com.my.httpapi.api.baseView.BasePresenter;
import com.tencent.captchasdk.TCaptchaDialog;
import com.tencent.captchasdk.TCaptchaVerifyListener;
import com.wswy.wyjk.api.Constant;
import com.wswy.wyjk.api.entity.DeviceMod;
import com.wswy.wyjk.api.entity.RequestParameter;
import com.wswy.wyjk.ui.login.Interface.ILogin;
import com.wswy.wyjk.ui.login.LoginActivity;
import com.wswy.wyjk.ui.login.model.CaptchaData;
import com.wswy.wyjk.ui.login.model.CaptchaValidData;
import com.wswy.wyjk.ui.login.model.LoginResponse;
import com.wswy.wyjk.ui.login.model.RequestLoginModel;
import com.wswy.wyjk.ui.login.model.SendPicResponse;
import com.wswy.wyjk.ui.login.model.VerificationPicResponse;
import com.wswy.wyjk.utils.GeetestUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class LoginPresenter extends BasePresenter<LoginActivity, RequestLoginModel> implements ILogin.IPresenter {

    @Override
    public void getCaptcha() {
        model.getCaptcha(RequestParameter.generate().getSingedParams(), request(data -> getView().getCaptcha((CaptchaData) data)));
    }

    @Override
    public void validCaptchaTc(Map<String, String> map) {
        model.validCaptchaTc(map, request(data -> getView().validCaptchaTc((CaptchaValidData) data)));
    }

    @Override
    public void code(Map<String, String> map) {
        model.code(map, request(data -> Toast.show("验证码发送成功")));
    }

    @Override
    public void picSend(Map<String, String> map) {
        model.picSend(map, request(data -> getView().geePic((SendPicResponse) data)));
    }

    @Override
    public void verSend(Map<String, String> map) {
        model.verSend(RequestParameter.generate().getSingedParams(map), request(data -> getView().verPic((VerificationPicResponse) data)));
    }

    @Override
    public void login(Map<String, String> map) {
        model.login(RequestParameter.generate().getSingedParams(map), request(data -> getView().login((LoginResponse) data)));
    }

    @Override
    public void timeEnd() {
        if (timer != null)
            timer.cancel();
    }

    @Override
    public void requestDevice(Map<String, String> map) {
        model.requestDevice(map, request(data -> getView().getUUID((DeviceMod) data)));
    }

    public void showTCDialog(CaptchaData captchaData, String phone) {
        if (Constant.TC.equals(captchaData.getVerifyType())) {
            TCaptchaDialog dialog = new TCaptchaDialog(getView(), Constant.TX007ID, captchaVerifyListener, null);
            dialog.show();
        } else if (Constant.PASS.equals(captchaData.getVerifyType())) {
            requestCode(phone, captchaData.getTicket());
        } else if (Constant.GEETEST.equals(captchaData.getVerifyType())) {
            GeetestUtils.customVerity(getView(), null, 1);
            getView().isGee(true);
        } else if (Constant.CAPTCHA.equals(captchaData.getVerifyType())) {
            requestPic();
        } else {
            Toast.show("手机号码被禁，无法发送验证码");
        }
    }

    private void validCaptchaTc(String appId, String ticket, String randstr) {
        Map<String, String> map = new HashMap<>();
        map.put("ticket", ticket);
        map.put("aid", appId);
        map.put("randstr", randstr);
        validCaptchaTc(RequestParameter.generate().getSingedParams(map));
    }

    public void requestCode(String phone, String ticket) {
        timer.start();
        Map<String, String> map = new HashMap<>();
        map.put("mobile", phone);
        map.put("codeType", "1");
        map.put("ticket", ticket);
        code(RequestParameter.generate().getSingedParams(map));
    }

    public void requestPic() {
        picSend(RequestParameter.generate().getSingedParams());
    }

    public TCaptchaVerifyListener captchaVerifyListener = jsonObject -> handleCallback(jsonObject);

    private void handleCallback(JSONObject jsonObject) {
        try {
            int ret = jsonObject.getInt("ret");
            String info;
            if (ret == 0) { //验证成功
                String ticket = jsonObject.getString("ticket");
                String appId = jsonObject.getString("appid");
                String randstr = jsonObject.getString("randstr");
                validCaptchaTc(appId, ticket, randstr);
            } else if (ret == -1001) {// 首个TCaptcha.js加载错误时允许用户(操作者)或业务方(接入方)介入重试
                info = String.format("验证码加载错误:%s", jsonObject.toString());
                Toast.show(info);
            } else { //用户(可能)点了验证码的关闭按钮
                info = String.format("用戶(可能)关闭验证码未验证成功:%s", jsonObject.toString());
                Toast.show(info);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {
        public void onTick(long millisUntilFinished) {
            getView().timeOnTick(millisUntilFinished);
        }

        public void onFinish() {
            getView().timeFinish();
        }
    };
}
